package com.youdoneed.friends.recommanderFacePicture;

import com.youdoneed.base.application.ResultMessage;
import com.youdoneed.base.common.JWTUtils;
import com.youdoneed.base.pagination.Page;
import com.youdoneed.friends.model.FaceNewsRelation;
import com.youdoneed.friends.model.RecommandFacePicture;
import com.youdoneed.friends.vo.IndexFacePictureVo;
import com.youdoneed.friends.vo.NewsVo;
import com.youdoneed.news.model.CmfPostsListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by lid on 2017/11/21 0021.
 * 推荐人脸照控制类
 */
@RestController
@RequestMapping("/recommandFacePicture")
@Api(description = "推荐人脸照控制类")
public class RecommandFacePictureController {

    private static final Logger logger = LoggerFactory.getLogger(RecommandFacePictureController.class);

    @Autowired
    private RecommandFacePictureService recommandFacePictureService;

    @RequestMapping(value = "/getAllPicturesByPage", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "后台分页获取所有推荐人脸照", value = "后台分页获取所有推荐人脸照")
    public ResultMessage<Page<RecommandFacePicture>> getAllPicturesByPage(Paging paging,
                                                                          @ApiParam("类型，新闻人物：newsmaker，" +
                                                                                  "推荐人脸照：recommandface")
                                                                          @RequestParam String type) {
        return new ResultMessage<>(recommandFacePictureService.getAllPicturesByPage(paging, type));
    }

    @RequestMapping(value = "/addPicture", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "后台添加推荐人脸照", value = "后台添加推荐人脸照")
    public ResultMessage<?> addPicture(@ApiParam("图片Url") @RequestParam String pictureUrl,
                                       @ApiParam("备注") @RequestParam String mark,
                                       @ApiParam("类型，新闻人物：newsmaker，" +
                                               "推荐人脸照：recommandface") @RequestParam String type,
                                       @ApiParam("是否显示，显示：1，不显示：0") @RequestParam Integer display) {
        recommandFacePictureService.addPicture(pictureUrl, mark, type, display);
        return new ResultMessage<>("保存推荐人脸照成功！");
    }

    @RequestMapping(value = "/deletePicture", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "删除推荐人脸照", value = "删除推荐人脸照")
    public ResultMessage<?> deletePicture(@ApiParam("图片id") @RequestParam String facePictureId) {
        recommandFacePictureService.deletePicture(facePictureId);
        return new ResultMessage<>("删除推荐人脸照成功！");
    }

    @RequestMapping(value = "/updatePicture", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "后台更新人脸照", value = "后台更新人脸照")
    public ResultMessage<?> updatePicture(@ApiParam("图片id") @RequestParam String facePictureId,
                                          @ApiParam("图片Url") @RequestParam(required = false) String pictureUrl,
                                          @ApiParam("备注") @RequestParam(required = false) String mark,
                                          @ApiParam("是否显示") @RequestParam(required = false) Integer display) {
        recommandFacePictureService.updatePicture(facePictureId, pictureUrl, mark, display);
        return new ResultMessage<>("修改推荐人脸照成功！");
    }

    @RequestMapping(value = "/changeSort", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "调整人脸照顺序", value = "调整人脸照顺序")
    public ResultMessage<?> changeSort(@ApiParam("图片id") @RequestParam String facePictureId,
                                       @ApiParam("方向！ up 是向上， down是向下") @RequestParam String direction) {
        recommandFacePictureService.changeSort(facePictureId, direction);
        return new ResultMessage<>("调整人脸照顺序成功！");
    }

    @RequestMapping(value = "/topPicture", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "置顶人脸照", value = "置顶人脸照")
    public ResultMessage<?> topPicture(@ApiParam("图片id") @RequestParam String facePictureId) {
        recommandFacePictureService.topPicture(facePictureId);
        return new ResultMessage<>("调整人脸照顺序成功！");
    }

    @RequestMapping(value = "/getIndexFacePicture", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "获取首页顶部照片", value = "获取首页顶部照片")
    public ResultMessage<List<IndexFacePictureVo>> getIndexFacePicture(@RequestHeader(value = "Authorization") String token) {
        String userId = JWTUtils.getTokenUserId(token);
        List<IndexFacePictureVo> listIndexFacePictureVo = recommandFacePictureService.getIndexFacePicture(userId);
        return new ResultMessage<List<IndexFacePictureVo>>(listIndexFacePictureVo).toSuccess();
    }

    @RequestMapping(value = "/addNewsForPictureId", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "为新闻人物添加新闻", value = "为新闻人物添加新闻")
    public ResultMessage<?> addNewsForPictureId(String facePictureId, @RequestBody List<NewsVo> listNewsVo) {
        recommandFacePictureService.addNewsForPictureId(facePictureId, listNewsVo);
        return new ResultMessage<>("添加新闻成功!");
    }

    @RequestMapping(value = "/deleteNewsForPictureId", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "为新闻人物删除新闻", value = "为新闻人物删除新闻")
    public ResultMessage<?> deleteNewsForPictureId(@RequestBody  List<String> listFaceNewsRelationIds) {
        recommandFacePictureService.deleteNewsForPictureId(listFaceNewsRelationIds);
        return new ResultMessage<>("删除新闻成功!");
    }

    @RequestMapping(value = "/getNewsByPictureId", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "根据照片获取新闻", value = "根据照片获取新闻")
    public ResultMessage<Page<FaceNewsRelation>> getNewsByPictureId(Paging paging, String facePictureId) {
        Page<FaceNewsRelation> listFaceNewsRelation = recommandFacePictureService.getNewsByPictureId(paging, facePictureId);
        return new ResultMessage<Page<FaceNewsRelation>>(listFaceNewsRelation).toSuccess();
    }

    @RequestMapping(value = "/deleteNewsByRelationId", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "删除热点人物的关联新闻", value = "删除热点人物的关联新闻")
    public ResultMessage<?> deleteNewsByRelationId(String faceNewsRelationId) {
        recommandFacePictureService.deleteNewsByRelationId(faceNewsRelationId);
        return new ResultMessage<>("删除新闻成功").toSuccess();
    }


    @RequestMapping(value = "/topRelationSort", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "置顶新闻", value = "置顶新闻")
    public ResultMessage<?> topRelationSort(@ApiParam("图片id") @RequestParam String faceNewsRelationId) {
        recommandFacePictureService.topRelationSort(faceNewsRelationId);
        return new ResultMessage<>("置顶新闻成功！");
    }


    @RequestMapping(value = "/changeRelationSort", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "调整新闻顺序", value = "调整新闻顺序")
    public ResultMessage<?> changeRelationSort(@ApiParam("图片id") @RequestParam String faceNewsRelationId,
                                       @ApiParam("方向！ up 是向上， down是向下") @RequestParam String direction) {
        recommandFacePictureService.changeRelationSort(faceNewsRelationId, direction);
        return new ResultMessage<>("调整新闻顺序成功！");
    }

    @RequestMapping(value = "/getPostsByPictureId", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "根据图片id获取新闻列表", value = "根据图片id获取新闻列表")
    public ResultMessage<List<CmfPostsListVo>> getPostsByPictureId(String pictureId){
        List<CmfPostsListVo> listCmfPostsListVo = recommandFacePictureService.getPostsByPictureId(pictureId);
        return new ResultMessage<List<CmfPostsListVo>>(listCmfPostsListVo).toSuccess();
    }
}
